package com.hanlink.dsp.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;

import com.qztech.sdk.R;
import com.qztech.sdk.ui.widget.SwipeMenu;
import com.qztech.sdk.ui.widget.SwipeMenuCreator;
import com.qztech.sdk.ui.widget.SwipeMenuItem;

/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C): 2015-11-15
 *
 * Description:
 *
 * Revision History
 * Date			  Author		Description
 * 2015-11-15        PeterVon       初始创建
 *
 * </pre>
 */
public class ToolUtil {

    public static int dp2px(int dp, Context context) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                context.getResources().getDisplayMetrics());
    }


    public static SwipeMenuCreator buildDeleteCreator(final Context context) {
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        context);
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                deleteItem.setWidth(ToolUtil.dp2px(90, context));
                deleteItem.setIcon(com.hanlink.res.R.drawable.ic_delete);
                menu.addMenuItem(deleteItem);
            }
        };
        return creator;
    }

    public static int packageCode(Context context) {
        PackageManager manager = context.getPackageManager();
        int code = 0;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            code = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }


    public static String getPackageName(Context context) {
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            return "V" + info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
